package com.xy.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xy.entity.LoanPeriod;
import com.xy.entity.User;
import com.xy.service.LoanPeriodService;

/**
 * 贷款周期控制层
 * 
 * @author 萧曳
 *
 */
@Controller
@Scope(value = "prototype")
public class LoanPeriodController {
	private Logger log = Logger.getLogger(LoanPeriodController.class);
	@Autowired
	private LoanPeriodService loanPeriodService;

	/**
	 * 添加贷款周期
	 * 
	 * @param loanPeriod
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "addLoanPeriod", method = RequestMethod.POST)
	public String addLoanPeriod(@Validated LoanPeriod loanPeriod, Model model, HttpSession session) {
		User user = (User) session.getAttribute("myUser");
		loanPeriod.setCreaterID(user.getID());
		loanPeriod.setCreaterTime(new Date());
		if (loanPeriodService.addLoanPeriod(loanPeriod)) {
			log.info("贷款周期添加成功!!" + loanPeriod);
		}
		return "selectLoanPeriod.action";

	}

	/*
	 * public String deleLoanPeriod(LoanPeriod loanPeriod) { if
	 * (loanPeriodService.deleLoanPeriod(loanPeriod)) {
	 * 
	 * } }
	 */
	/**
	 * 更新贷款周期状态 ajax
	 * 
	 * @param loanPeriod
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "updateLoanPeriodStatus", method = RequestMethod.GET)
	public @ResponseBody String updateLoanPeriodStatus(LoanPeriod loanPeriod, HttpSession session) {
		User user = (User) session.getAttribute("myUser");
		List<LoanPeriod> loanPeriodList = loanPeriodService.selectLoanPeriod(loanPeriod);
		loanPeriod = loanPeriodList.get(0);
		loanPeriod.setCreaterID(user.getID());
		loanPeriod.setCreaterTime(new Date());
		if (loanPeriod.getLoanPeriodStatus() == 0) {
			loanPeriod.setLoanPeriodStatus(1);
		} else {
			loanPeriod.setLoanPeriodStatus(0);
		}
		if (loanPeriodService.updateLoanPeriod(loanPeriod)) {
			return "success";
		} else {
			return "error";
		}
	}

	/**
	 * 更新贷款周期信息
	 * 
	 * @param loanPeriod
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "updateLoanPeriod", method = RequestMethod.POST)
	public String updateLoanPeriod(LoanPeriod loanPeriod, HttpSession session) {
		User user = (User) session.getAttribute("myUser");
		loanPeriod.setCreaterID(user.getID());
		loanPeriod.setCreaterTime(new Date());
		if (loanPeriodService.updateLoanPeriod(loanPeriod)) {
			log.info("贷款周期更新成功!!" + loanPeriod);
		} else {
			log.info("贷款周期更新失败!!" + loanPeriod);
		}
		return "selectLoanPeriod.action";
	}

	/**
	 * 查询所有贷款周期
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "selectLoanPeriod", method = { RequestMethod.GET, RequestMethod.POST })
	public String selectLoanPeriod(Model model) {
		List<LoanPeriod> loanPeriodList = loanPeriodService.selectLoanPeriod(null);
		model.addAttribute("loanPerionList", loanPeriodList);
		return "pages/backend/loanTimeManage.jsp";
	}
}
